
package CustomerGUIs;

import Model.InternalDBManager;
import Model.Checks;
import Model.Customer;
import Supermarket.LoginUser;
import javax.persistence.*;
import javax.swing.ImageIcon;

/**
 *
 * @author bagi
 */
public class ListOfChecksForm extends javax.swing.JFrame {

    private static EntityManager em;
    private LoginUser loginUser;
    Customer customer;
    
    public ListOfChecksForm() {
       em = InternalDBManager.getEm();
       initComponents();
       this.setIconImage(new
                ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage()); 
    }
    
    public ListOfChecksForm(LoginUser loginUser) {
       this.loginUser = loginUser;
       em = InternalDBManager.getEm();
       //αναζητεί και επιστρέφει τον πελάτη με βάση το userName
       customer=findCustomerInDB(loginUser.getUserName());
       //σχεδιάζει και ενημερώνει την φόρμα
       initComponents();
       //Θέτει το ονοματεπώνυμο του πελάτη στο αντίστοιχο πεδίο της φόρμας
       CustomerNameField.setText(customer.getFirstName()+" "+
               customer.getLastName());
       this.setIconImage(new
                ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage()); 
    }
        
    //βρίστκει τον πελάτη στη ΒΔ 
    private Customer findCustomerInDB(String userName){
        if (!em.getTransaction().isActive()){
           em.getTransaction().begin();
        }  
        //ψάχνει τον πελάτη με το userName
        Customer c_exists = (Customer)em.createNamedQuery("Customer.findByPointsCardNumber")
                .setParameter("pointsCardNumber", userName).getSingleResult();   
        return c_exists;
    } 
    
    /**
     * This method is called from within the constructor to initialize the form.
     
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        dateFormatRenderer1 = new Supermarket.DateFormatRenderer();
        checksQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT c FROM Checks c WHERE c.customer =:customer").setParameter("customer",customer);
        checksList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : checksQuery.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListOfChecks = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        OKButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        CustomerNameField = new javax.swing.JTextField();

        dateFormatRenderer1.setText("dateFormatRenderer1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Πίνακας Επιταγών");
        setName("checkFrame"); // NOI18N
        setResizable(false);

        ListOfChecks.setAutoCreateRowSorter(true);
        ListOfChecks.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        ListOfChecks.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ListOfChecks.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        ListOfChecks.setNextFocusableComponent(OKButton);
        ListOfChecks.setRowHeight(20);
        ListOfChecks.setRowSelectionAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, checksList, ListOfChecks);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${checkId}"));
        columnBinding.setColumnName("Αρ.Επιταγής");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${timeStamp}"));
        columnBinding.setColumnName("Ημερομηνία");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${status}"));
        columnBinding.setColumnName("Έγκυρη");
        columnBinding.setColumnClass(Boolean.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(ListOfChecks);
        if (ListOfChecks.getColumnModel().getColumnCount() > 0) {
            ListOfChecks.getColumnModel().getColumn(0).setResizable(false);
            ListOfChecks.getColumnModel().getColumn(0).setPreferredWidth(30);
            ListOfChecks.getColumnModel().getColumn(1).setResizable(false);
            ListOfChecks.getColumnModel().getColumn(1).setPreferredWidth(80);
            ListOfChecks.getColumnModel().getColumn(1).setCellRenderer(dateFormatRenderer1);
            ListOfChecks.getColumnModel().getColumn(2).setResizable(false);
            ListOfChecks.getColumnModel().getColumn(2).setPreferredWidth(80);
        }

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Επιταγές:");

        OKButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        OKButton.setActionCommand("OK");
        OKButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        OKButton.setLabel("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Ονοματεπώνυμο πελάτη:");

        CustomerNameField.setEditable(false);
        CustomerNameField.setBackground(new java.awt.Color(204, 204, 204));
        CustomerNameField.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        CustomerNameField.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        CustomerNameField.setDisabledTextColor(new java.awt.Color(0, 0, 153));
        CustomerNameField.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 288, Short.MAX_VALUE)
                                .addComponent(OKButton))
                            .addComponent(jSeparator1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(CustomerNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CustomerNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OKButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
   
    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_OKButtonActionPerformed
       
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListOfChecksForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListOfChecksForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListOfChecksForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListOfChecksForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListOfChecksForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CustomerNameField;
    private javax.swing.JTable ListOfChecks;
    private javax.swing.JButton OKButton;
    private java.util.List<Checks> checksList;
    private javax.persistence.Query checksQuery;
    private Supermarket.DateFormatRenderer dateFormatRenderer1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
